package com.qiya.LiaoDa.kb;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.qiya.LiaoDa.MainActivity;
import com.qiya.LiaoDa.R;

public class UpdateDialog extends AppCompatActivity implements View.OnClickListener {

    private EditText et_alName;
    private EditText et_alSex;
    private EditText et_alAge;
    private Button btn_submit;
    private SP altStu=null;
    DBOperate dbOperate=new DBOperate();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_dialog);
        et_alName=(EditText)findViewById(R.id.et_alName);
        et_alSex=(EditText)findViewById(R.id.et_alSex);
        et_alAge=(EditText)findViewById(R.id.et_alAge);
        btn_submit=(Button)findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(this);
        initEditText();
        dbOperate.test(this);
    }

    private void initEditText(){
        Bundle bundle=getIntent().getExtras();
        altStu=(SP) bundle.get("altStu");
        if(altStu!=null){
            et_alName.setText(altStu.getName());
            et_alSex.setText(altStu.getSex());
            et_alAge.setText(String.valueOf(altStu.getAge()));
        }
    }
    @Override
    public void onClick(View v) {

        if ((! TextUtils.isEmpty(et_alAge.getText()) )
                && (! TextUtils.isEmpty(et_alName.getText()) )&&
                (! TextUtils.isEmpty(et_alSex.getText()) )) {
            String name = et_alName.getText().toString();
            String sex = et_alSex.getText().toString();
            int age = Integer.parseInt(et_alAge.getText().toString());
            if (altStu != null) {
                altStu.setName(name);
                altStu.setSex(sex);
                altStu.setAge(age);
                dbOperate.update(altStu);
            }
            Intent intent = new Intent(UpdateDialog.this, MainActivity.class);
            intent.putExtra("id",1);
            startActivity(intent);;
        }
        else {
            Toast.makeText(this, "请输入必要的信息", Toast.LENGTH_SHORT).show();
        }
    }
}